﻿using Mapster.Models;
using Photo.Core.Attribute;
using Photo.Core.Dtos.Dto;
using Photo.Core.EnumSummary.System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Photo.Core.Utils
{
    /// <summary>
    /// 枚举公共方法
    /// </summary>
    public static class EnumUtil
    {
        /// <summary>
        /// 枚举转KeyValueDto
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static List<KeyValueDto> EnumToList<TEnum>()
        {
            List<KeyValueDto> list = new List<KeyValueDto>();
            foreach (var item in Enum.GetValues(typeof(TEnum)))
            {
                var listItem = new KeyValueDto()
                {
                    Key = ((int)item).ToString(),
                    Value = item.ToString(),
                    Description = item.ToDescription(),
                    Sort = item.GetEnumSort(),
                };
                list.Add(listItem);
            }
            list = list.OrderBy(o => o.Sort).ToList();
            return list;
        }

        /// <summary>
        /// 枚举转KeyValueDto
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<KeyValueDto> EnumToList(this Type type)
        {
            List<KeyValueDto> list = new List<KeyValueDto>();
            foreach (object item in Enum.GetValues(type))
            {
                var listItem = new KeyValueDto()
                {
                    Key = ((int)item).ToString(),
                    Value = item.ToString(),
                    Description = item.ToDescription(),
                    Sort = item.GetEnumSort(),
                };
                list.Add(listItem);
            }
            list = list.OrderBy(o => o.Sort).ToList();
            return list;
        }

        /// <summary>
        /// 枚举获取描述
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string ToDescription(this Enum val)
        {
            var memberInfo = val.GetType().GetMember(val.ToString());

            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes == null || attributes.Length != 1)
            {
                //如果没有定义描述，就把当前枚举值的对应名称返回
                return val.ToString();
            }

            return (attributes.Single() as DescriptionAttribute).Description;
        }


        #region 私有方法
        /// <summary>
        /// 枚举获取描述
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        private static string ToDescription(this object val)
        {
            var memberInfo = val.GetType().GetMember(val.ToString());

            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes == null || attributes.Length != 1)
            {
                //如果没有定义描述，就把当前枚举值的对应名称返回
                return val.ToString();
            }

            return (attributes.Single() as DescriptionAttribute).Description;
        }

        /// <summary>
        /// 枚举获取排序
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        private static int GetEnumSort(this object val)
        {
            var memberInfo = val.GetType().GetMember(val.ToString());

            var attributes = memberInfo[0].GetCustomAttributes(typeof(EnumSortAttribute), false);

            if (attributes == null || attributes.Length != 1)
            {
                //如果没有定义描述，就把当前枚举值的对应值返回
                return Convert.ToInt32(val);
            }

            return (attributes.Single() as EnumSortAttribute).Sort;
        }
        #endregion
    }
}
